from conn_mysql import ApiSql
from .is_password import test
from sendsms import is_phone
import datetime
class Register(ApiSql):

    def verify(self,phone,password,cpassword,verify_code,methods):

        if methods !='POST':

            return {'status':-1,'errorCode':1,'errorMsg':'请检查你的方法应该为POST','resultBody':None}

        if  not all((phone,password,cpassword,verify_code)):

            return {'status':-1,'errorCode':2,'errorMsg':'请检查你的参数','resultBody':None}

        if not is_phone(phone):

            return {'status':-1,'errorCode':4,'errorMsg':'请检查你的手机号','resultBody':None}
        if password !=cpassword:

            return {'status': -1,'errorCode': 7,'errorMsg': '两次密码不一致','resultBody':None}

        if not test(password):

            return {'status': -1,'errorCode': 3,'errorMsg': '密码过于简单','resultBody': None}

        phones=self.compare_phone(phone)
        
        if phones==None:

            info=self.select_verify_code(verify_code,phone)

            if info:
                time_delta=datetime.timedelta(minutes=5)
                register=info['register_time']
                if (time_delta+register)<datetime.datetime.now():

                    return {
                        "status": -1,
                        "errorCode": 2,
                        "errorMsg": '验证码超时，请重新申请新验证码',
                        "resultBody": None,
                    }
                    
                result=self.insert_login_info(phone,password)

                return {'status': 1,'errorCode': None,'errorMsg': None,'resultBody': '注册成功'}

            else:

                return {'status': -1,'errorCode': 6,'errorMsg': '验证码不正确','resultBody': None}
        else:

            return {'status': -1,'errorCode': 5,'errorMsg': '手机号已注册','resultBody': None}



            



            




            



        

        






        



